﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FITClub.Episode1.Controller.AI;

namespace FITClub.LevelBuilder
{
    public partial class WaypointForm : Form
    {
        public Waypoints Waypoints { get; private set; }

        private LevelBuilderControl builder;

        public WaypointForm(LevelBuilderControl lbc)
        {
            this.builder = lbc;
            InitializeComponent();
            NewWaypoints();
        }

        private void NewWaypoints()
        {
            Waypoints = new Waypoints();
            InvalidateSource();
        }

        internal void InvalidateSource()
        {
            this.listPropertyGrid1.DataSource = Waypoints.AllWaypoints.Values.ToList();
#if WAYPOINTS
            this.builder.Waypoints = this.Waypoints;
#endif
        }

        private void listPropertyGrid1_AddRequest(Type obj)
        {
            string name = string.Format("waypoint{0}", this.Waypoints.AllWaypoints.Count);
            this.Waypoints.AllWaypoints.Add(name, new WPNode()
            {
                Behaviour = (NodeBahaviour)this.comboBox1.SelectedValue,
                Name = name,
                Position = new Microsoft.Xna.Framework.Vector2((float)this.numericBackgX.Value, (float)numericBackgY.Value)
            });
            InvalidateSource();
        }

        private void listPropertyGrid1_RemoveRequest(Type arg1, System.Collections.IList arg2)
        {
            foreach (WPNode item in arg2)
            {
                this.Waypoints.Remove(item.Name);
            }
            InvalidateSource();
        }

        private void listPropertyGrid1_SelectionChanged(ListPropertyGrid obj)
        {
            IEnumerable<object> items = obj.SelectedItems;
            if (items != null)
            {
                this.builder.SelectedNode = items.First() as WPNode;
                this.builder.Invalidate();
                if (items.Count() == 1)
                {
                    if (listBox1.Items.Count >= 2)
                    {
                        listBox1.Items.Clear();
                    }
                    listBox1.Items.AddRange(items.ToArray());
                }
                if (items.Count() >= 2)
                {
                    this.listBox1.Items.Clear();
                    foreach (WPNode item in items)
                    {
                        if (listBox1.Items.Count < 2)
                        {
                            listBox1.Items.Add(item);
                        }
                    }
                }
            }
            this.buttonDis.Enabled = this.listBox1.Items.Count >= 2;
            this.button1conn.Enabled = this.listBox1.Items.Count >= 2;
            this.button2conn.Enabled = this.listBox1.Items.Count >= 2;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.NewWaypoints();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                this.saveFileDialog1.FileName = this.textBox1.Text;
                if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.Waypoints.Save(saveFileDialog1.FileName);
                    this.textBox1.Text = saveFileDialog1.FileName;
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.openFileDialog1.FileName = this.textBox1.Text;                
                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    using (var fs = openFileDialog1.OpenFile())
                    {
                        this.Waypoints = new Waypoints(fs);
                    }
                    this.textBox1.Text = openFileDialog1.FileName;
                    InvalidateSource();
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }
        }

        private void WaypointForm_Load(object sender, EventArgs e)
        {
            this.comboBox1.DataSource = Enum.GetValues(typeof(NodeBahaviour));
            this.comboBox1.SelectedIndex = 0;
        }

        private void WaypointForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void button1conn_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count >= 2)
            {
                (listBox1.Items[0] as WPNode).ConnectTo((listBox1.Items[1] as WPNode));
            }
        }

        private void button2conn_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count >= 2)
            {
                WPNode.Connect((listBox1.Items[0] as WPNode), (listBox1.Items[1] as WPNode));
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count >= 2)
            {
                (listBox1.Items[0] as WPNode).Disconnect((listBox1.Items[1] as WPNode));
            }
        }

        private void dijkstraToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WPNode node = (listBox1.Items[0] as WPNode);
            this.Waypoints.Dijkstra(node);
            this.builder.ShowDijkstra = true;
            this.builder.Invalidate();
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            e.Cancel = this.listBox1.Items.Count != 1;
        }
    }
}